home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9566 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Passing by reference and Returning structures
  5. Date: 11 Mar 1996 20:57:53 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4i244h$9ke@sparcserver.lrz-muenchen.de>
  9. References: <4hvc3b$avn@news2.acs.oakland.edu> <4i1nkc$ov9@hobbes.cc.uga.edu>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. greear@pollux.cs.uga.edu (Ben Greear) writes:
  13. >In article <4hvc3b$avn@news2.acs.oakland.edu>,
  14. >Monument <jggoslin@vela.acs.oakland.edu> wrote:
  15. >>
  16. >>My question is, what does the return for a structure look like.  I
  17. >>would assume the function prototype would look something like:
  18. >>
  19. >>my_struct my_function(int matrix[])
  20.  
  21. >try my_struct& my_function(int matrix[]);
  22. >             ^ being the important part
  23.  
  24. This is _not_ a valid function declaration in C. In might be a valid
  25. function declaration in a similar but different language. 
  26.  
  27. Even using a _valid_ declaration like 
  28.  
  29.   my_struct *my_function(int matrix[]);
  30.  
  31. it is debateable whether the gain in efficientcy is worth the worry
  32. about the validity of the pointer to the returned struct, because
  33. that pointer must either point to a static object, in which case
  34. unexpected things may happen the next time this function is called,
  35. or to allocated memory, in which case the calling function is 
  36. responsible for freeing that memory exactly once. For reasonable
  37. small structs, plain and simple returning the "whole" struct is
  38. _not_ the worst approach. Even for larger structs, it is up to the
  39. compiler whether it will use a special calling mechanism for this
  40. case.
  41.  
  42. >>
  43. >>and the call would be something like
  44. >>
  45. >>structure=my_function(matrix)
  46. >>
  47. >>where my_struct is the structure type and matrix is the variable size
  48. >>matrix.  However, when I tried just such a setup it didn't work, gave
  49.  
  50. The name "matrix" implies something that is _not_ a vector. Could this
  51. be related to the problem of the original poster?
  52.  
  53. Kurt
  54. --
  55. | Kurt Watzka                             Phone : +49-89-2180-6254
  56. | watzka@stat.uni-muenchen.de
  57. | ua302aa@sunmail.lrz-muenchen.de
  58.